var roleHBRU_I = require('role.HBRU_I');
var roleE10S58_H_I = require('role.E10S58_H_I');
var roleE8S57_HBR_I = require('role.E8S57_HBR_I');
var roleE8S57_R_I = require('role.E8S57_R_I');
var roleE8S57_HBR_II = require('role.E8S57_HBR_II');
var roleE8S57_fighter_AH = require('role.E8S57_fighter_AH');
var roleE9S57_HBR_I = require('role.E9S57_HBR_I');
var roleE9S57_HBR_II = require('role.E9S57_HBR_II');
var roleE9S57_R_I = require('role.E9S57_R_I');
var roleE9S57_fighter_AH = require('role.E9S57_fighter_AH');
//var roleE9S59_HBR_I = require('role.E9S59_HBR_I'); 
var roleHar = require('role.har'); 
var roleHar_o = require('role.har_o'); 
//var roleHarout = require('role.harout');
//var roleHarout_E9S59 = require('role.harout_E9S59');
var roleStander = require('role.stander');
//var roleStander_E9S59 = require('role.stander_E9S59');
var roleStander_E8S57 = require('role.stander_E8S57');
var roleFighter = require('role.fighter');
var roleFighter_AH = require('role.fighter_AH');
var roleQiqiang = require('role.qiqiang');
var roleMushi = require('role.mushi');
var roleCarrier = require('role.carrier');
//var roleCarrierlink = require('role.carrierlink');
var roleCarrierdrop = require('role.carrierdrop');//兼职link
var roleUpgrader = require('role.upgrader');
var roleRepairer = require('role.repairer');
var roleBuilder = require('role.builder');
var roleBuilderout = require('role.builderout');
var roleTransporter = require('role.transporter');
var roleTransportertotower = require('role.transportertotower');
var roleHario = require('role.hario');
var roleCarrierio = require('role.carrierio');

// var roleUpgraderio = require('role.upgraderio');
// var roleBuilderio = require('role.builderio');

module.exports.loop = function () {
    for(var name in Memory.creeps) {
        if(!Game.creeps[name]) {
            delete Memory.creeps[name];
            console.log('Clearing non-existing creep memory:', name);
        }
    }
//LINK大约17tick执行一次
    console.log(Game.time)
    if(Game.time % 17 == 0){ 
        const linkFrom = Game.rooms['E9S58'].lookForAt('structure',10, 16)[0];
        const linkFrom2 = Game.rooms['E9S58'].lookForAt('structure',35, 26)[0];
        const linkTo = Game.rooms['E9S58'].lookForAt('structure',29, 22)[0];
        linkFrom.transferEnergy(linkTo);
        linkFrom2.transferEnergy(linkTo);
        if(Game.cpu.bucket == 10000) {
            Game.cpu.generatePixel();
        }
    }

    var tower = Game.getObjectById('62199c4b15cd3edd278efbf8');
    if(tower) {
        var closestHostile1 = tower.room.find(FIND_HOSTILE_CREEPS);
        if(closestHostile1.length>0) {
            tower.attack(closestHostile1[0]);
        }
        else{
            var closestDamagedStructure1 = tower.pos.findClosestByRange(FIND_STRUCTURES, {
                filter: (structure) => (structure.hits < structure.hitsMax*0.5 && 
                    structure.structureType != STRUCTURE_WALL && structure.structureType != STRUCTURE_RAMPART|| 
                    structure.hits < 11000 && structure.structureType == STRUCTURE_RAMPART ||
                    structure.hits < 11000 && structure.structureType == STRUCTURE_WALL)});
            if(closestDamagedStructure1) {
                tower.repair(closestDamagedStructure1);
            }
        }
    }

    var tower = Game.getObjectById('621772c77fa0bc59655e0f4a');
    if(tower) {
        var closestHostile2 = tower.room.find(FIND_HOSTILE_CREEPS);
        if(closestHostile2.length>0) {
            tower.attack(closestHostile2[closestHostile2.length-1]);
        }
        else{
            var closestDamagedStructure2 = tower.pos.findClosestByRange(FIND_STRUCTURES, {
                filter: (structure) => (structure.hits < structure.hitsMax*0.5 && 
                    structure.structureType != STRUCTURE_WALL && structure.structureType != STRUCTURE_RAMPART|| 
                    structure.hits < 11000 && structure.structureType == STRUCTURE_RAMPART ||
                    structure.hits < 11000 && structure.structureType == STRUCTURE_WALL)});
            if(closestDamagedStructure2) {
                tower.repair(closestDamagedStructure2);
            }
        }
    }
    
/*    var tower = Game.getObjectById('621f3c0d5613fbdd2db6d900');
    if(tower) {
        var closestHostile3 = tower.room.find(FIND_HOSTILE_CREEPS);
        if(closestHostile3.length>0) {
            tower.attack(closestHostile3[0]);
        }
        else{
            var closestDamagedStructure3 = tower.pos.findClosestByRange(FIND_STRUCTURES, {
                filter: (structure) => (structure.hits < structure.hitsMax*0.9 && 
                    structure.structureType != STRUCTURE_WALL && structure.structureType != STRUCTURE_RAMPART|| 
                    structure.hits < 21000 && structure.structureType == STRUCTURE_RAMPART ||
                    structure.hits < 21000 && structure.structureType == STRUCTURE_WALL)});
            if(closestDamagedStructure3) {
                tower.repair(closestDamagedStructure3);
            }
        }
    }

    if(Game.spawns['Spawn1'].spawning) { 
        var spawningCreep = Game.creeps[Game.spawns['Spawn1'].spawning.name];
        Game.spawns['Spawn1'].room.visual.text(
            '🛠️' + spawningCreep.memory.role,
            Game.spawns['Spawn1'].pos.x + 1, 
            Game.spawns['Spawn1'].pos.y, 
            {align: 'left', opacity: 0.8});
    }

    for(var name in Game.creeps) {
        var creep = Game.creeps[name];
        if(creep.memory.role == 'hbru_i') {
            roleHBRU_I.run(creep);
        }
        if(creep.memory.role == 'e10s58_h_i') {
            roleE10S58_H_I.run(creep);
        }
        if(creep.memory.role == 'e8s57_hbr_i') {
            roleE8S57_HBR_I.run(creep);
        }
        if(creep.memory.role == 'e8s57_r_i') {
            roleE8S57_R_I.run(creep);
        }
        if(creep.memory.role == 'e8s57_hbr_ii') {
            roleE8S57_HBR_II.run(creep);
        }
        if(creep.memory.role == 'e8s57_fighter_ah') {
            roleE8S57_fighter_AH.run(creep);
        }
        if(creep.memory.role == 'e9s57_hbr_i') {
            roleE9S57_HBR_I.run(creep);
        }
        if(creep.memory.role == 'e9s57_hbr_ii') {
            roleE9S57_HBR_II.run(creep);
        }
        if(creep.memory.role == 'e9s57_r_i') {
            roleE9S57_R_I.run(creep);
        }
        if(creep.memory.role == 'e9s57_fighter_ah') {
            roleE9S57_fighter_AH.run(creep);
        }
        // var creep = Game.creeps[name];
        // if(creep.memory.role == 'e9s59_hbr_i') {
        //     roleE9S59_HBR_I.run(creep);
        // }
        if(creep.memory.role == 'har') {
            roleHar.run(creep);
        }
        if(creep.memory.role == 'har_o') {
            roleHar_o.run(creep);
        }
        // if(creep.memory.role == 'harout') {
        //     roleHarout.run(creep);
        // }
        // if(creep.memory.role == 'harout_e9s59') {
        //     roleHarout_E9S59.run(creep);
        // }
        if(creep.memory.role == 'stander') {
            roleStander.run(creep);
        }
        // if(creep.memory.role == 'stander_e9s59') {
        //     roleStander_E9S59.run(creep);
        // }
        if(creep.memory.role == 'stander_e8s57') {
             roleStander_E8S57.run(creep);
        }
        if(creep.memory.role == 'fighter') {
            roleFighter.run(creep);
        }
        if(creep.memory.role == 'fighter_ah') {
            roleFighter_AH.run(creep);
        }
        // if(creep.memory.role == 'qiqiang') {
        //     roleQiqiang.run(creep);
        // }
        // if(creep.memory.role == 'mushi') {
        //     roleMushi.run(creep);
        // }
        // if(creep.memory.role == 'carrier') {
        //     roleCarrier.run(creep);
        // }
        // if(creep.memory.role == 'carrierlink') {
        //     roleCarrierlink.run(creep);
        // }
        if(creep.memory.role == 'carrierdrop') {
            roleCarrierdrop.run(creep);
        }
        if(creep.memory.role == 'upgrader') {
            roleUpgrader.run(creep);
        }
        if(creep.memory.role == 'repairer') {
            roleRepairer.run(creep);
        }
        if(creep.memory.role == 'builder') {
            roleBuilder.run(creep);
        }
        if(creep.memory.role == 'builderout') {
            roleBuilderout.run(creep);
        }
        // if(creep.memory.role == 'builderout_e9s59') {
        //     roleBuilderout_E9S59.run(creep);
        // }
        if(creep.memory.role == 'transporter') {
            roleTransporter.run(creep);
        }
        if(creep.memory.role == 'transportertotower') {
            roleTransportertotower.run(creep);
        }
        if(creep.memory.role == 'hario') {
            roleHario.run(creep);
        }
        if(creep.memory.role == 'carrierio') {
            roleCarrierio.run(creep);
        }
        // if(creep.memory.role == 'upgraderio') {
        //     roleUpgraderio.run(creep);
        // }
        // if(creep.memory.role == 'builderio') {
        //     roleBuilderio.run(creep);
        // }
    }

    //数量检测和出生功能
    if(Game.time % 11 == 0){
        //在E9S59进化测
        var hbru_i = _.filter(Game.creeps, (creep) => creep.memory.role == 'hbru_i');
        console.log('hbru_i: ' + hbru_i.length);
        if(hbru_i.length<0) {
            var newName = 'hbru_i' + Game.time;
            console.log('Spawning new hbru_i: ' + newName);
        Game.spawns['Spawn2'].spawnCreep([WORK,WORK,WORK,WORK,WORK,WORK,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,{memory: {role: 'hbru_i'}}); 
        }

        var e10s58_h_i = _.filter(Game.creeps, (creep) => creep.memory.role == 'e10s58_h_i');
        console.log('e10s58_h_i: ' + e10s58_h_i.length);
        if(e10s58_h_i.length < 0) {
            var newName = 'e10s58_h_i' + Game.time;
            console.log('Spawning new e10s58_h_i: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,WORK,WORK,WORK,
            CARRY,CARRY,
            MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'e10s58_h_i'}});
        }

        var e8s57_r_i = _.filter(Game.creeps, (creep) => creep.memory.role == 'e8s57_r_i');
        console.log('e8s57_r_i: ' + e8s57_r_i.length);
        if(e8s57_r_i.length < 0) {
            var newName = 'e8s57_r_i' + Game.time;
            console.log('Spawning new e8s57_r_i: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,WORK,
            CARRY,CARRY,CARRY,CARRY,
            MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'e8s57_r_i'}});
        }
        
        var e8s57_hbr_i = _.filter(Game.creeps, (creep) => creep.memory.role == 'e8s57_hbr_i');
        console.log('e8s57_hbr_i: ' + e8s57_hbr_i.length);
        if(e8s57_hbr_i.length < 0) {
            var newName = 'e8s57_hbr_i' + Game.time;
            console.log('Spawning new e8s57_hbr_i: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,
            CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,
            MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'e8s57_hbr_i'}});
        }
        
        var e8s57_hbr_ii = _.filter(Game.creeps, (creep) => creep.memory.role == 'e8s57_hbr_ii');
        console.log('e8s57_hbr_ii: ' + e8s57_hbr_ii.length);
        if(e8s57_hbr_ii.length < 0) {
            var newName = 'e8s57_hbr_ii' + Game.time;
            console.log('Spawning new e8s57_hbr_ii: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,
            CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,
            MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'e8s57_hbr_ii'}});
        }
        
        var e8s57_fighter_ah = _.filter(Game.creeps, (creep) => creep.memory.role == 'e8s57_fighter_ah');
        console.log('e8s57_fighter_ah: ' + e8s57_fighter_ah.length);
        if(e8s57_fighter_ah.length < 0) {
            var newName = 'e8s57_fighter_ah' + Game.time;
            console.log('Spawning new e8s57_fighter_ah: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([RANGED_ATTACK,RANGED_ATTACK,RANGED_ATTACK,RANGED_ATTACK,RANGED_ATTACK,RANGED_ATTACK,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'e8s57_fighter_ah'}});
        }
        
        var stander_e8s57 = _.filter(Game.creeps, (creep) => creep.memory.role == 'stander_e8s57');
        console.log('Stander_e8s57: ' + stander_e8s57.length);
        if(stander_e8s57.length <0) {
            var newName = 'stander_e8s57' + Game.time;
            console.log('Spawning new stander_e8s57: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([CLAIM,CLAIM,MOVE,MOVE], newName,
                {memory: {role: 'stander_e8s57'}});
        }

        // var stander_e9s59 = _.filter(Game.creeps, (creep) => creep.memory.role == 'stander_e9s59');
        // console.log('Stander_e9s59: ' + stander_e9s59.length);
        // if(stander_e9s59.length < 0) {
        //     var newName = 'stander_e9s59' + Game.time;
        //     console.log('Spawning new stander_e9s59: ' + newName);
        //     Game.spawns['Spawn1'].spawnCreep([CLAIM,MOVE,MOVE], newName,
        //         {memory: {role: 'stander_e9s59'}});
        // }
    
        var stander = _.filter(Game.creeps, (creep) => creep.memory.role == 'stander');
        console.log('Stander: ' + stander.length);
        if(stander.length < 0) {
            var newName = 'stander' + Game.time;
            console.log('Spawning new stander: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([CLAIM,CLAIM,MOVE,MOVE], newName,
                {memory: {role: 'stander'}});
        } 
        
        var e9s57_hbr_i = _.filter(Game.creeps, (creep) => creep.memory.role == 'e9s57_hbr_i');
        console.log('e9s57_hbr_i: ' + e9s57_hbr_i.length);
        if(e9s57_hbr_i.length < 0) {
            var newName = 'e9s57_hbr_i' + Game.time;
            console.log('Spawning new e9s57_hbr_i: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,
            CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,
            MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'e9s57_hbr_i'}});
        }

        var e9s57_hbr_ii = _.filter(Game.creeps, (creep) => creep.memory.role == 'e9s57_hbr_ii');
        console.log('e9s57_hbr_ii: ' + e9s57_hbr_ii.length);
        if(e9s57_hbr_ii.length < 0) {
            var newName = 'e9s57_hbr_ii' + Game.time;
            console.log('Spawning new e9s57_hbr_ii: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,
            CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,
            MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'e9s57_hbr_ii'}});
        }
        
        var e9s57_r_i = _.filter(Game.creeps, (creep) => creep.memory.role == 'e9s57_r_i');
        console.log('e9s57_r_i: ' + e9s57_r_i.length);
        if(e9s57_r_i.length < 0) {
            var newName = 'e9s57_r_i' + Game.time;
            console.log('Spawning new e9s57_r_i: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,WORK,
            CARRY,CARRY,CARRY,CARRY,
            MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'e9s57_r_i'}});
        }
              
        var e9s57_fighter_ah = _.filter(Game.creeps, (creep) => creep.memory.role == 'e9s57_fighter_ah');
        console.log('e9s57_fighter_ah: ' + e9s57_fighter_ah.length);
        if(e9s57_fighter_ah.length < 0) {
            var newName = 'e9s57_fighter_ah' + Game.time;
            console.log('Spawning new e9s57_fighter_ah: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([RANGED_ATTACK,RANGED_ATTACK,RANGED_ATTACK,RANGED_ATTACK,RANGED_ATTACK,RANGED_ATTACK,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'e9s57_fighter_ah'}});
        }

        // var e9s59_hbr_i = _.filter(Game.creeps, (creep) => creep.memory.role == 'e9s59_hbr_i');
        // console.log('e9s59_hbr_i: ' + e9s59_hbr_i.length);
        // if(e9s59_hbr_i.length < 0) {
        //     var newName = 'e9s59_hbr_i' + Game.time;
        //     console.log('Spawning new e9s59_hbr_i: ' + newName);
        //     Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE], newName,
        //         {memory: {role: 'e9s59_hbr_i'}});
        // }

        var fighter = _.filter(Game.creeps, (creep) => creep.memory.role == 'fighter');
        console.log('fighter: ' + fighter.length);
        if(fighter.length < 0) {
            var newName = 'fighter' + Game.time;
            console.log('Spawning new fighter: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([ATTACK,ATTACK,ATTACK,ATTACK,ATTACK,ATTACK,ATTACK,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'fighter'}});
        }  
  
        var fighter_ah = _.filter(Game.creeps, (creep) => creep.memory.role == 'fighter_ah');
        console.log('fighter_ah: ' + fighter_ah.length);
        if(fighter_ah.length < 0) {
            var newName = 'fighter_ah' + Game.time;
            console.log('Spawning new fighter_ah: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([RANGED_ATTACK,RANGED_ATTACK,RANGED_ATTACK,RANGED_ATTACK,RANGED_ATTACK,RANGED_ATTACK,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'fighter_ah'}});
        }

        // var qiqiang = _.filter(Game.creeps, (creep) => creep.memory.role == 'qiqiang');
        // console.log('qiqiang: ' + qiqiang.length);
        // if(qiqiang.length < 0) {
        //     var newName = 'qiqiang' + Game.time;
        //     console.log('Spawning new qiqiang: ' + newName);
        //     Game.spawns['Spawn1'].spawnCreep([TOUGH,TOUGH,TOUGH,TOUGH,TOUGH,TOUGH,TOUGH,TOUGH,TOUGH,TOUGH,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
        //         {memory: {role: 'qiqiang'}});
        // }

        // var mushi = _.filter(Game.creeps, (creep) => creep.memory.role == 'mushi');
        // console.log('mushi: ' + mushi.length);
        // if(mushi.length < 0) {
        //     var newName = 'mushi' + Game.time;
        //     console.log('Spawning new mushi: ' + newName);
        //     Game.spawns['Spawn1'].spawnCreep([HEAL,HEAL,HEAL,HEAL,MOVE,MOVE,MOVE,MOVE], newName,
        //         {memory: {role: 'mushi'}});
        // }

        var har = _.filter(Game.creeps, (creep) => creep.memory.role == 'har');
        console.log('Har: ' + har.length);
        if(har.length < 1) { 
            var newName = 'har' + Game.time;
            console.log('Spawning new har: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,WORK,WORK,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'har'}});
        }
        
        var har_o = _.filter(Game.creeps, (creep) => creep.memory.role == 'har_o');
        console.log('Har_o: ' + har_o.length);
        if(har_o.length < 1 && Game.getObjectById('5bbcb32740062e4259e94141').mineralAmount != 0) { 
            var newName = 'har_o' + Game.time;
            console.log('Spawning new har_o: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'har_o'}});
        }
 
        // var harout = _.filter(Game.creeps, (creep) => creep.memory.role == 'harout');
        // console.log('Harout: ' + harout.length);
        // if(harout.length < 0) {
        //     var newName = 'harout' + Game.time;
        //     console.log('Spawning new harout: ' + newName);
        //     Game.spawns['Spawn1'].spawnCreep([WORK,WORK,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
        //         {memory: {role: 'harout'}});
        // }

        // var harout_e9s59 = _.filter(Game.creeps, (creep) => creep.memory.role == 'harout_e9s59');
        // console.log('Harout_e9s59: ' + harout_e9s59.length);
        // if(harout_e9s59.length < 0) {
        //     var newName = 'harout_e9s59' + Game.time;
        //     console.log('Spawning new harout: ' + newName);
        //     Game.spawns['Spawn1'].spawnCreep([WORK,WORK,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
        //         {memory: {role: 'harout_e9s59'}});
        // }

       

        // var carrier = _.filter(Game.creeps, (creep) => creep.memory.role == 'carrier');
        // console.log('carrier: ' + carrier.length);
        // if(carrier.length < 0) {
        //     var newName = 'carrier' + Game.time;
        //     console.log('Spawning new carrier: ' + newName);
        //     Game.spawns['Spawn1'].spawnCreep([CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE], newName,
        //         {memory: {role: 'carrier'}});
        // }

        // var carrierlink = _.filter(Game.creeps, (creep) => creep.memory.role == 'carrierlink');
        // console.log('carrierlink: ' + carrierlink.length);
        // if(carrierlink.length < 0) {
        //     var newName = 'carrierlink' + Game.time;
        //     console.log('Spawning new carrierlink: ' + newName);
        //     Game.spawns['Spawn1'].spawnCreep([CARRY,CARRY,MOVE], newName,
        //         {memory: {role: 'carrierlink'}});
        // }

        var carrierdrop = _.filter(Game.creeps, (creep) => creep.memory.role == 'carrierdrop');
        console.log('carrierdrop: ' + carrierdrop.length);
        if(carrierdrop.length < 1) {
            var newName = 'carrierdrop' + Game.time;
            console.log('Spawning new carrierdrop: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'carrierdrop'}});
        }
        
        var upgrader = _.filter(Game.creeps, (creep) => creep.memory.role == 'upgrader');
        console.log('upgrader: ' + upgrader.length);
        if(upgrader.length < 3) {//6
            var newName = 'upgrader' + Game.time;
            console.log('Spawning new upgrader: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'upgrader'}});
        }

        var repairer = _.filter(Game.creeps, (creep) => creep.memory.role == 'repairer');
        console.log('repairer: ' + repairer.length);
        if(repairer.length < 1) {
            var newName = 'repairer' + Game.time;
            console.log('Spawning new repairer: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,WORK,WORK,WORK,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'repairer'}});
        }

        var builder = _.filter(Game.creeps, (creep) => creep.memory.role == 'builder');
        console.log('builder: ' + builder.length);
        if(builder.length < 1) {
            var newName = 'builder' + Game.time;
            console.log('Spawning new builder: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,WORK,WORK,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'builder'}});
        } 

         var builderout = _.filter(Game.creeps, (creep) => creep.memory.role == 'builderout');
         console.log('builderout: ' + builderout.length);
         if(builderout.length <0) {
             var newName = 'builderout' + Game.time;
             console.log('Spawning new builderout: ' + newName);
             Game.spawns['Spawn1'].spawnCreep([WORK,WORK,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE], newName,
                 {memory: {role: 'builderout'}});
          

        // var builderout_e9s59 = _.filter(Game.creeps, (creep) => creep.memory.role == 'builderout_e9s59');
        // console.log('builderout_e9s59: ' + builderout_e9s59.length);
        // if(builderout_e9s59.length < 0) {
        //     var newName = 'builderout_e9s59' + Game.time;
        //     console.log('Spawning new builderout_e9s59: ' + newName);
        //     Game.spawns['Spawn1'].spawnCreep([WORK,WORK,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE], newName,
        //         {memory: {role: 'builderout_e9s59'}});
        // }

        var transporter = _.filter(Game.creeps, (creep) => creep.memory.role == 'transporter');
        console.log('transporter: ' + transporter.length);
        if(transporter.length < 1) {
            var newName = 'transporter' + Game.time;
            console.log('Spawning new transporter: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'transporter'}});
        }

        var transportertotower = _.filter(Game.creeps, (creep) => creep.memory.role == 'transportertotower');
        console.log('transportertotower: ' + transportertotower.length);
        if(transportertotower.length < 1) {
            var newName = 'transportertotower' + Game.time;
            console.log('Spawning new transportertotower: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'transportertotower'}});
        }

        var hario = _.filter(Game.creeps, (creep) => creep.memory.role == 'hario');
        console.log('Hario: ' + hario.length);
        if(hario.length < 1) {
            var newName = 'hario' + Game.time;
            console.log('Spawning new hario: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,WORK,WORK,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'hario'}});
        } 
  
        var carrierio = _.filter(Game.creeps, (creep) => creep.memory.role == 'carrierio');
        console.log('carrierio: ' + carrierio.length);
        if(carrierio.length<0){
            var newName = 'carrierio' + Game.time;
            console.log('Spawning new carrierio: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([CARRY,CARRY,MOVE], newName,
                {memory: {role: 'carrierio'}});
        }
    // var upgraderio = _.filter(Game.creeps, (creep) => creep.memory.role == 'upgraderio');
        // console.log('upgraderio: ' + upgraderio.length);
        // if(upgraderio.length < 0) {
        //     var newName = 'upgraderio' + Game.time;
        //     console.log('Spawning new upgraderio: ' + newName);
        //     Game.spawns['Spawn1'].spawnCreep([WORK,WORK,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE], newName,
        //         {memory: {role: 'upgraderio'}});
        // }

        // var builderio = _.filter(Game.creeps, (creep) => creep.memory.role == 'builderio');
        // console.log('builderio: ' + builderio.length);
        // if(builderio.length < 0) {
        //     var newName = 'builderio' + Game.time;
        //     console.log('Spawning new builderio: ' + newName);
        //     Game.spawns['Spawn1'].spawnCreep([WORK,WORK,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE], newName,
        //         {memory: {role: 'builderio'}});
        //
        */
    }}